package com.example.myapplication.view

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.databinding.DataBindingUtil
import com.example.myapplication.R
import com.example.myapplication.databinding.LayoutNumberComboBigBinding
import com.example.myapplication.databinding.ViewGuideSlideLeftMessageBinding

/**
 * @author zijian.cheng
 * @date 2019/8/19
 */
class GiftGroupNumComboLayout(context: Context?, attrs: AttributeSet?) : FrameLayout(context!!, attrs) {
    private val animatorSet = AnimatorSet()
    private var animatorScale1 = arrayOf(3f, 0.5f, 1f)
    private var animatorScale2 = arrayOf(2.2f, 0.5f, 1f)
    private var animatorScale3 = arrayOf(2f, 0.5f, 1f)
    private var animatorScale4 = arrayOf(1.8f, 0.5f, 1f)
    var binding : LayoutNumberComboBigBinding? = null
    init {
        binding = DataBindingUtil.bind(View.inflate(context, R.layout.layout_number_combo_big, this))
        val objectAnimatorX = ObjectAnimator.ofFloat(binding!!.idRootView, "scaleX", 3f, 0.8f, 1f)
        val objectAnimatorY = ObjectAnimator.ofFloat(binding!!.idRootView, "scaleY", 3f, 0.8f, 1f)
        animatorSet.duration = 250
        animatorSet.playTogether(objectAnimatorX, objectAnimatorY)
    }

    fun numberCombo(number: Long) {
        binding!!.idNumberContent.removeAllViews()
        number.toString().forEach {
            val child = ImageView(context)
            child.scaleType = ImageView.ScaleType.FIT_XY
            val layout = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
            child.layoutParams = layout
            child.setImageDrawable(getDrawable(context, getResForNumber(it.toString().toInt())))
            binding!!.idNumberContent.addView(child)
        }
        var animatorScale: Array<Float>
        if (number >= 1000) {
            animatorScale = animatorScale4
        } else if (number >= 100) {
            animatorScale = animatorScale3
        } else if (number >= 10) {
            animatorScale = animatorScale2
        } else {
            animatorScale = animatorScale1
        }
        if (animatorSet.isRunning) {
            animatorSet.cancel()
        }


        val objectAnimatorX = ObjectAnimator.ofFloat(binding!!.idRootView, "scaleX", animatorScale[0], animatorScale[1], animatorScale[2])
        val objectAnimatorY = ObjectAnimator.ofFloat(binding!!.idRootView, "scaleY", animatorScale[0], animatorScale[1], animatorScale[2])
        animatorSet.duration = 300
        animatorSet.playTogether(objectAnimatorX, objectAnimatorY)

        animatorSet.start()
    }

    fun getDrawable(context: Context, resId: Int): Drawable? {
        return context.resources.getDrawable(resId)
    }


    private fun getResForNumber(number: Int): Int {
        return R.mipmap.img_continuou_0 + number
    }
}